<?php

namespace app\modules\models;

use Yii;

/**
 * This is the model class for table "{{%admin}}".
 *
 * @property string $id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $logintime
 * @property string $loginip
 * @property string $createtime
 */
class Admin extends \yii\db\ActiveRecord
{
    public $rememberMe=true;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%admin}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [

            ['username','required','message'=>'用户名不能为空','on'=>['login','seekpass']],
            ['password','required','message'=>'密码不能为空','on'=>'login'],
            ['rememberMe','boolean','on'=>'login'],
            ['password','validataPass','on'=>'login'],
            [['logintime', 'loginip', 'createtime'], 'integer'],
            [['username', 'password'], 'string', 'max' => 32],
            [['email'], 'email','on' => 'seekpass'],
            [['email'], 'required', 'on'=>'seekpass'],
            [['email'], 'validataEmail', 'on'=>'seekpass'],
//            [['username', 'password'], 'unique', 'targetAttribute' => ['username', 'password'], 'message' => 'The combination of Username and Password has already been taken.'],
//            [['username', 'email'], 'unique', 'targetAttribute' => ['username', 'email'], 'message' => 'The combination of Username and Email has already been taken.'],
        ];
    }
    public function validataPass(){
        if (!$this->hasErrors()){
            $user=self::find()->where('username = :user and password = :pass',[":user"=>$this->username,":pass"=>md5($this->password)])->one();
            if (is_null($user))
                $this->addError('password','密码错误');
        }
    }
    public function validataEmail(){
        if (!$this->hasErrors()){
            $user=self::find()->where('username = :user and email = :email',[":user"=>$this->username,":email"=>$this->email])->one();
            if (is_null($user))
                $this->addError('email','email错误！');
        }
    }

    /**
     * 登录
     * @param $post
     * @return bool
     */
    public function login($post){
        $this->scenario = 'login';
        if ($this->load($post) && $this->validate()){//验证
            $rem=$this->rememberMe ? 24*3600 :0 ;
            $session=Yii::$app->session;
            session_set_cookie_params($rem);
            $session['admin']=[
                'admin'=>$this->username,
                'isLogin'=>1,
                'logintime'=>date('YmdHis')
            ];
            $this->updateAll(['logintime'=>time(),'loginip'=>ip2long(Yii::$app->request->getUserIP())],"username = :user",[':user' => $this->username ]);
            return (boolean)$session['admin']['isLogin'];
        }
        return false;
    }


    public function seekpass($data){
        $this->scenario='seekpass';
        if ($this->load($data)&&$this->validate()){
            //邮箱验证成功
            //发送邮件
            $mailer=Yii::$app->mailer->compose('seekpass');
            $mailer->setFrom('15222291959@163.com');
            $mailer->setTo($this->email);
            $mailer->setSubject('酷客玩-密码找回');
            $mailer->send();
            return $mailer->send();
        }
        return false;
    }
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'username' => 'Username',
            'password' => 'Password',
            'email' => 'Email',
            'logintime' => 'Logintime',
            'loginip' => 'Loginip',
            'createtime' => 'Createtime',
        ];
    }
}